#include<iostream>
using namespace std;
#include<vector>
class Solution {
public:
    long long validSubstringCount(string word1, string word2) {
        int arr[26] = { 0 }, count = 0;
        long long ans = 0;
        for (auto& e : word2)
            if (++arr[e - 'a'] == 1)
                ++count;
        for (int left = 0, right = 0; right < word1.size(); ++right) {
            char now = word1[right] - 'a';
            --arr[now];
            if (arr[now] == 0)
                --count;
            while (count == 0) {
                ans += word1.size() - right;
                now = word1[left++] - 'a';
                if (++arr[now] == 1)
                    ++count;
            }
        }
        return ans;
    }
};